package edu.ccdb.irods.methods;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;

import edu.ccdb.irods.signature.SwingWorkerExtension;
import edu.ccdb.irods.signature.UploadStatus;
import edu.sdsc.grid.io.irods.IRODSFile;
import edu.sdsc.grid.io.irods.IRODSFileOutputStream;

public class CCDBMethods {

	public static void ccdbCopyFrom (IRODSFile destinationFile, File localFile, boolean forceOverwrite, UploadStatus status, 
																		SwingWorkerExtension thread) throws IOException {
		
		// process
		IRODSFileOutputStream output = new IRODSFileOutputStream(destinationFile);
		InputStream input =  new BufferedInputStream( new FileInputStream(localFile) );
		
		long length = 0;
		byte[] buffer = new byte[ Configuration.BUFFER_SIZE ];
		int bytesRead = 0;
		
		while ( length < localFile.length() ) {
			// read n bytes from file to the buffer
			bytesRead = input.read( buffer, 0, Configuration.BUFFER_SIZE);
			output.write( buffer , 0, bytesRead );
			
			// update status
			status.setSingleFileStatus(localFile.getPath(), BigInteger.valueOf(  bytesRead ) );

			// signal the GUI to update
			thread.setText("Uploading " + localFile.getName() );
			thread.setProgressSignal( status.getTotatlUploadedPercent() );
			
			length += (bytesRead);
		}
		
		// checksum
		
		input.close();
		output.close();		
	}	
}
